package com.netease.nie.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.netease.nie.base.exceptions.UnexpectedException;
import com.netease.nie.base.utils.CodecUtils;
import com.netease.nie.base.utils.CustomWebUtils;
import com.netease.nie.dto.UserLoginDto;

/**
 * 用户登录帮助类
 * @author wwbn3695
 * @createDate July 22nd,2016
 *
 */
public class UserLoginUtils {
	
	private static Logger logger = Logger.getLogger(UserLoginUtils.class);

	private static final String NTES_YD_SESS = "NTES_YD_SESS";
	
	/**
	 * 获取用户登录状态
	 * @param request
	 * @return
	 */
	public static UserLoginDto getUserLoginDto(HttpServletRequest request) {
		
		
		return null;
	}
	
	public static UserLoginDto getUserLoginDto(HttpServletRequest request, String cookieValue) {
		return null;
	}
	
	/**
	 * 校验用户ip
	 * @param request
	 * @return
	 */
	private static boolean validateUserIp(HttpServletRequest request, String userIp) {
		String remoteIp = CustomWebUtils.getIp(request);
		logger.info("userIp = " + userIp);
		logger.info("remoteIp = " + remoteIp);
		/*if (remoteIp.equals(userIp))
			return true;
		logger.info("校验ip不成功");
		return false;*/
		return true;
	}
	
	
	/**
	 * 设置手机号登录cookie
	 * @param response
	 * @param cookieValue
	 */
	public static void addNeteaseMobileCookie(HttpServletResponse response, String cookieValue) {
		CustomWebUtils.addCookieValue(response, NTES_YD_SESS, cookieValue);
	}
	
	/**
	 * 删除手机号登录cookie
	 * @param request
	 * @param response
	 */
	public static void deleteNeteaseMobileCookie(HttpServletRequest request, HttpServletResponse response) {
		CustomWebUtils.deleteCookieValue(request, response, NTES_YD_SESS);
	}
	
	/**
	 * 生成userToken
	 * @param userLoginDto
	 * @return
	 */
	public static String generateUserToken(UserLoginDto userLoginDto) {
		if (userLoginDto == null)
			return StringUtils.EMPTY;
		String ssn = userLoginDto.getSsn();
		String mobile = userLoginDto.getMobile();
		if (StringUtils.isBlank(ssn) || StringUtils.isBlank(mobile))
			return StringUtils.EMPTY;
		String userToken = CodecUtils.hexMD5(ssn + mobile);
		return userToken;
	}
	
	/**
	 * SDK签名
	 * @return
	 */
	public static String sign(String apiKey, Long timeStamp, String pathQS) {
		if (StringUtils.isBlank(apiKey))
			throw new UnexpectedException("apiKey为空");
		String stringToSign = generateStringToSign("GET", timeStamp, "", pathQS);
		logger.info("stringToSign = " + stringToSign);
		if (StringUtils.isBlank(stringToSign))
			throw new UnexpectedException("签名参数为空");
			
		try {
			logger.info("签名参数: apiKey = " + apiKey + ", stringToSign = " + stringToSign);
			byte[] binaryRaw = CodecUtils.hmacSHA1Encrypt(apiKey, stringToSign);
			return CodecUtils.encodeBASE64Str(binaryRaw);
		} catch (Exception e) {
			logger.error("签名失败", e.getCause());
			throw new UnexpectedException("签名失败");
		}
	}
	
	/**
	 * SDK签名token
	 * @param httpMethod
	 * @param timeStamp
	 * @param httpBody
	 * @param optFields
	 * @return
	 */
	private static String generateStringToSign(String httpMethod, Long timeStamp, String httpBody, String pathQS) {
		if (StringUtils.isBlank(httpMethod))
			return null;
		if (timeStamp == null || timeStamp == 0)
			return null;
		if (StringUtils.isBlank(pathQS))
			return null;
		String token = httpMethod + "\n" + timeStamp + "\n" + httpBody + "\n" + pathQS;
		return token;
	}
	
}
